package com.changgou.goods.controller;

import com.changgou.goods.pojo.Template;
import com.changgou.goods.service.TemplateService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/28 4:03 下午
 * @Version 1.0
 * @description xxx
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/template")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    /**
     * 根据分类ID查询模板信息
     * @param cid
     * @return
     */
    @GetMapping(value = "/category/{cid}")
    public Result<Template> findTemplateByCid(@PathVariable(value = "cid") Integer cid) {
        Template byCategoryInfo = templateService.findByCategoryId(cid);
        return new Result<>(true, StatusCode.OK, "查询成功", byCategoryInfo);
    }
    /**
     * 添加模板数据
     * @param template
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody Template template) {
        int num = templateService.add(template);
        return new Result(true, StatusCode.OK, "添加成功", num);
    }

    /**
     * 根据ID删除模板信息：
     * @param id
     * @return
     */
    @DeleteMapping(value = "/del/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int delete = templateService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功", delete);
    }

    /**
     * 更新模板数据：
     * @param template
     * @param id
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody Template template, @PathVariable Integer id) {
        int update = templateService.update(template, id);
        return new Result(true, StatusCode.OK, "更新成功", update);
    }

    /**
     * 查询所有模板数据
     * @return
     */
    @GetMapping
    public Result<List<Template>> findAll() {
        List<Template> list = templateService.fincAll();
        return new Result<List<Template>>(true, StatusCode.OK, "查询所有模板数据成功", list);
    }

    /**
     * 根据id查询模板成功
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Template> findById(@PathVariable Integer id) {
        Template template = templateService.findById(id);
        return new Result<Template>(true, StatusCode.OK, "查询成功", template);
    }

    /**
     * 多条件搜索查询：
     * @param template
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Template>> findList(@RequestBody(required = false) Template template) {
        List<Template> list = templateService.findList(template);
        return new Result<List<Template>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询：
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<List<Template>> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Template> pageInfo = templateService.findPage(page, size);
        return new Result<List<Template>>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件查询带分页：
     * @param template
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<List<Template>> findPage(@RequestBody(required = false) Template template, @PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Template> pageInfo = templateService.findPage(template, page, size);
        return new Result<List<Template>>(true, StatusCode.OK, "多条件查询带分页", pageInfo);
    }

}
